home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / NewStringArt-c / GenStringNums.c next >
Encoding:
C/C++ Source or Header  |  1994-12-04  |  3.4 KB  |  157 lines  |  [TEXT/MMCC]

  1. /*    gennums.c    generate number tables for stringart    */
  2.  
  3. #include <stdio.h>        /* standard I/O */
  4. #include <fixmath.h>        /* floating point math */
  5. #include <math.h>        //• Added by KAL.
  6. /*
  7.     This demo creates random vector designs.  This is accomplished by
  8.     randomly choosing a function for each coordinate halve of the two
  9.     points describing a vector that moves through two dimensional
  10.     space. Both x coordinate halves cannot be the same since the design
  11.     would simply be a collection of vertical lines. Similarly both
  12.     y coordinate halves cannot be the same.
  13.  
  14.     The functions are:
  15.  
  16.     function[0][x] = sin( 2 * PI * x / numLines )
  17.     function[1][x] = -sin( 2 * PI * x / numLines )
  18.     function[2][x] = cos( 2 * PI * x / numLines ) 
  19.     function[3][x] = -cos( 2 * PI * x / numLines ) 
  20.     function[4][x] = sin( 4 * PI * x / numLines )
  21.     function[5][x] = -sin( 4 * PI * x / numLines )
  22.     function[6][x] = cos( 4 * PI * x / numLines ) 
  23.     function[7][x] = -cos( 4 * PI * x / numLines ) 
  24.     function[8][x] = sin( 6 * PI * x / numLines )
  25.     function[9][x] = -sin( 6 * PI * x / numLines )
  26.     function[10][x] = cos( 6 * PI * x / numLines ) 
  27.     function[11][x] = -cos( 6 * PI * x / numLines ) 
  28.     function[12][x] = 2 * abs( x - (numLines / 2) - 1 )
  29.  
  30.     The values of the functions were pre computed to have the demo
  31.     run as fast as possible.  The program will only terminate on 
  32.     interrupt since it is in an endless loop.
  33. */
  34.  
  35. /* these should be in a common header file, but I'm lazy */
  36. #define numLines    343        /* number of vectors in a design */
  37. #define num_functions    12        /* number of functions */
  38.  
  39. int p [numLines];
  40.  
  41. main()
  42. {
  43.     int i, j, k, l, m;
  44.     FILE *f;
  45.  
  46.     f = fopen ("NewStringNums.c", "w");
  47.     fprintf (f, "\n\t/* This source file generated by gennums.c */\n\n\n");
  48.     fprintf (f, "#define num_functions %d\n", num_functions);
  49.     fprintf (f, "#define numLines %d\n\n", numLines);
  50.     fprintf (f, "int p [num_functions][numLines] = {\n\n");
  51.     for (i = 0;  i < num_functions;  i++) 
  52.     {
  53.         cypher (i, 1024);
  54.         
  55.         /* scaling down is faster with a power of 2 */
  56.         DumpArray (f, i);
  57.     }
  58.     fprintf (f, "\t}\n};\n\n");
  59.     fclose (f);
  60. }
  61.  
  62. DumpArray (f, n)    FILE *f;    int n;
  63. {
  64.     int i;
  65.     
  66.     if (n != 0)
  67.         fprintf (f, "\t},\n\n");
  68.     fprintf (f, "\t{\n");
  69.     
  70.     for (i = 0; i < numLines; i++) 
  71.     {
  72.         fprintf (f, "\t%d", p[i]);
  73.         if (i != numLines - 1)
  74.             fprintf (f, ",");
  75.         if (i % 7 == 6)
  76.             fprintf (f, "\n");
  77.     }
  78. }
  79.         
  80.  
  81. #define PI 3.1415927
  82.  
  83. /* Hey Uncle Jed... */
  84. int cypher (int func, int scale)
  85. {
  86.     float tmp;
  87.     int x;
  88.     
  89.     for (x = 0; x < numLines; x++) 
  90.     {
  91.             /* clumsy, but doesn't need to be efficient */
  92.         switch (func) 
  93.         {
  94.             case 0:
  95.                 tmp = sin( 2 * PI * x / numLines);
  96.             break;
  97.             
  98.             case 1:
  99.                 tmp = -sin( 2 * PI * x / numLines);
  100.             break;
  101.             
  102.             case 2:
  103.                 tmp = cos( 2 * PI * x / numLines);
  104.             break;
  105.             
  106.             case 3:
  107.                 tmp = -cos( 2 * PI * x / numLines);
  108.             break;
  109.             
  110.             case 4:
  111.                 tmp = sin( 4 * PI * x / numLines);
  112.             break;
  113.             
  114.             case 5:
  115.                 tmp = -sin( 4 * PI * x / numLines);
  116.             break;
  117.             
  118.             case 6:
  119.                 tmp = cos( 4 * PI * x / numLines);
  120.             break;
  121.             
  122.             case 7:
  123.                 tmp = -cos( 4 * PI * x / numLines);
  124.             break;
  125.             
  126.             case 8:
  127.                 tmp = sin( 6 * PI * x / numLines);
  128.             break;
  129.             
  130.             case 9:
  131.                 tmp = -sin( 6 * PI * x / numLines);
  132.             break;
  133.             
  134.             case 10:
  135.                 tmp = cos( 6 * PI * x / numLines);
  136.             break;
  137.             
  138.             case 11:
  139.                 tmp = -cos( 6 * PI * x / numLines);
  140.             break;
  141.             
  142.             case 12:
  143.                 tmp = 2 * abs( x - (numLines / 2) - 1); /* not used */
  144.             break;
  145.             
  146.             default:
  147.                 tmp = x;    /* shouldn't happen */
  148.             break;
  149.         }
  150.         p [x] = (int)(tmp * scale);
  151.     }
  152. }
  153.  
  154.  
  155.  
  156.  
  157.